package com.tencent.cloud.iov.util.backdoor;

import android.content.Context;
import android.view.View;
import android.widget.Toast;

/**
 * 开启调试模式：连续点击 5 次 "首页" 标签项
 *
 * @author showxiao
 * @date 2018/9/14
 */
public class OpenDebugListener implements View.OnClickListener {

    private static final int MIN_CLICK_COUNT = 5;

    private Context mContext;

    private int mContinuousClickCount = 0;
    private long mLastClickTime = 0;

    public OpenDebugListener(Context context) {
        mContext = context.getApplicationContext();
    }

    @Override
    public void onClick(View v) {
        if (DebugBackdoor.isDebugMode()) {
            return;
        }

        long curTime = System.currentTimeMillis();
        if (mLastClickTime == 0 || curTime - mLastClickTime < 500) {
            ++mContinuousClickCount;

            if (mContinuousClickCount >= MIN_CLICK_COUNT) {
                Toast.makeText(mContext, "已开启调试模式", Toast.LENGTH_SHORT).show();
                DebugBackdoor.setDebugMode(true);

                mContinuousClickCount = 0;
            }
        } else {
            mContinuousClickCount = 0;
        }

        mLastClickTime = curTime;
    }
}
